package com.tanhua.server.controller;

import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.pojo.Answers;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tanhua")
public class TodayBestController {

    @Autowired
    private TodayBestService todayBestService;


    /**
     * 查询今日佳人
     *
     * @return
     */
    @GetMapping("todayBest")
    public TodayBest queryTodayBest() {
        return this.todayBestService.queryTodayBest();
    }
    /*以前版本
    public TodayBest queryTodayBest(@RequestHeader("Authorization") String token){
        return this.todayBestService.queryTodayBest(token);
    }*/

    /**
     * 推荐列表
     *
     * @return
     */
    @GetMapping("recommendation")
    public PageResult queryRecommendUserList(RecommendUserQueryParam queryParam) {
        return this.todayBestService.queryRecommendUserList(queryParam);
    }
    /*以前版本
    public PageResult queryRecommendUserList(RecommendUserQueryParam queryParam, @RequestHeader("Authorization") String token) {
        return this.todayBestService.queryRecommendUserList(queryParam, token);
    }*/

    /**
     * 查询今日佳人详情
     *
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryTodayBest(@PathVariable("id") Long userId) {
        try {
            TodayBest todayBest = this.todayBestService.queryTodayBest(userId);
            return ResponseEntity.ok(todayBest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId) {
        try {
            String question = this.todayBestService.queryQuestion(userId);
            return ResponseEntity.ok(question);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回复陌生人问题
     *
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> param) {
        try {
            Long userId = Long.valueOf(param.get("userId").toString());
            String reply = param.get("reply").toString();
            Boolean result = this.todayBestService.replyQuestion(userId, reply);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender", required = false) String gender,
                                                          @RequestParam(value = "distance", defaultValue = "2000") String distance) {
        try {
            List<NearUserVo> list = this.todayBestService.queryNearUser(gender, distance);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 探花
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList() {
        try {
            List<TodayBest> list = this.todayBestService.queryCardsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.todayBestService.likeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> disLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.todayBestService.disLikeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 发送传音
     *
     * @param soundFile
     * @return
     */
    @PostMapping("peachblossom")
    public String sendVoice(@RequestParam(value = "soundFile", required = false) MultipartFile soundFile) {
        boolean b = this.todayBestService.saveSound(soundFile);
        if (!b) {
            throw new BusinessException("今日次数已用完");
        }
        return "上传成功";
    }


    /**
     * 接收传音
     *
     * @return
     */
    //要获取一个异性，不是自己的，不是重复的声音对象
    @GetMapping("peachblossom")
    public SoundVo getVoice() {
        SoundVo soundVo = this.todayBestService.querySound();
        if (soundVo == null) {
            throw new BusinessException("今日次数已用完");
        }
        return soundVo;
    }


    /**
     * 问卷列表
     * @return
     */
    @GetMapping("testSoul")
    public List<QuestionnaireVo> SoulQuestionnaire() {
        List<QuestionnaireVo> questionnaireVos = this.todayBestService.SQuestionnaireLike();
        return questionnaireVos;
    }

    /**
     * 提交问卷
     *
     * @param answers
     */
    @PostMapping("testSoul")
    public void TestSoul(@RequestBody Map<String, List<Answers>> answers) {
        List<Answers> answersList = answers.get("answers");
        this.todayBestService.testSoul(answersList);
    }

    /**
     * 测试结果
     * @param id
     * @return
     */
    @GetMapping("testSoul/report/{id}")
    public ResponseEntity<TestResultVo> TestResult(@PathVariable("id") Long id) {
        try {
            TestResultVo testResultVo = this.todayBestService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
